<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">供应商列表</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="供应商名称" prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入供应商名称检索"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            class="btn-success"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="supplierAdd()"
            v-hasPermi="['system:supplierInfo:add']"
          >新建</el-button>
        </el-col>
        <el-col :span="21">
          <el-button
            class="btn-scanCodeDel"
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="btnExport()"
          >导出</el-button>
        </el-col>
        <el-col :span="1">
          <el-popover placement="bottom" title="筛选列" trigger="click" width="40">
            <el-checkbox-group v-model="checkedColumns" size="mini">
              <el-checkbox v-for="item in checkBoxGroup" :key="item" :label="item" :value="item"></el-checkbox>
            </el-checkbox-group>
            <div title="筛选列" class="filter-table-col" slot="reference">
              <img src="@/assets/img/screening.png" width="25" />
            </div>
          </el-popover>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="supplierList" @row-click="handleView" :default-sort="{prop: 'date', order: 'descending'}" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="35" align="center" />
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-for="(item, index) in tableHeaderData">
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :align="item.align"
            :sortable="item.sortable"
            :show-overflow-tooltip="true"
            :key="index"
            :width="item.width"
            v-if="colData[index].isTrue"
          >
            <template slot-scope="scope">
              <template v-if="item.label == '纳税比例类型'">
                {{ scope.row.shareInvoice == null ? '' : scope.row.shareInvoice.taxpayerScale == '0' ? '13%' : scope.row.shareInvoice.taxpayerScale == '1' ? '9%' : scope.row.shareInvoice.taxpayerScale == '2' ? '6%' : scope.row.shareInvoice.taxpayerScale == '3' ? '3%' : scope.row.shareInvoice.taxpayerScale == '4' ? '1%' : '' }}
              </template>
              <template v-else-if="item.label == '审核状态'">
                <div v-if="scope.row.approveStatus == 0" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.approveStatus == 1" style="color:#52C41A">已审核</div>
                <div v-if="scope.row.approveStatus == 2" style="color:#fd562c">反审核</div>
                <div v-if="scope.row.approveStatus == 3" style="color:#fd562c">拒绝</div>
              </template>
              <template v-else-if="item.label == '状态'">
                <div v-if="scope.row.status == 2" style="color:#fd562c">禁用</div>
                <div v-if="scope.row.status == 1" style="color:#52C41A">正常</div>
              </template>
              <span v-else>{{ scope.row[scope.column.property] }}</span>
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click.native.stop="handleDelete(scope.row)"
              v-hasPermi="['system:supplierInfo:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { listSupplier, deleteSupplier } from "@/api/system/basedata/supplier/supplier"
import { listSupplierlist } from "@/api/system/basedata/supplierlist/supplierlist"
import { exportSupplier } from "@/api/system/export/export"

export default {
  name: "plan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 生产列表数据
      supplierList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue:undefined
      },
      type: '',
      id: '',
      list: '',
      ids: [],
      tableHeaderData: [
        { label: "编号", align: "center", prop: "supCode", width: "150", sortable: true },
        { label: "名称", align: "center", prop: 'name', width: "150" },
        { label: "简称", align: "center", prop: "shortName"},
        { label: "助记码", align: "center", prop: "mnemonicCode"},
        { label: "供应商分类", align: "center", prop: "sortName"},
        { label: "纳税比例类型", align: "center", prop: "taxpayerScale", width: "120"  },
        { label: "审核状态", align: "center", prop: "approveStatus"},
        { label: "状态", align: "center", prop: "status" },
        { label: "备注", align: "center", prop: "remark" },
      ],
      colData: [
        { title: "编号", isTrue: true },
        { title: "名称", isTrue: true },
        { title: "简称", isTrue: true },
        { title: "助记码", isTrue: true },
        { title: "供应商分类", isTrue: true },
        { title: "纳税比例类型", isTrue: true },
        { title: "审核状态", isTrue: true },
        { title: "状态", isTrue: true },
        { title: "备注", isTrue: true },
      ],
      // 当前选中的多选框，代表当前展示的列
      checkedColumns: ["编号", "名称", "简称", "助记码", "供应商分类", "纳税比例类型", "审核状态", "状态", "备注"],
      // 多选框的列表，列出表格的每一列
      checkBoxGroup: ["编号", "名称", "简称", "助记码", "供应商分类", "纳税比例类型", "审核状态", "状态", "备注"]
    }
  },
  created() {
    if(this.$route.query.type){
      this.type = this.$route.query.type
      this.id = this.$route.query.id
      this.list = this.$route.query.list
      this.getSearchList()
    }else{
      this.getList()
    }
  },
  watch: {
    checkedColumns(val) {
      let arr = this.checkBoxGroup.filter(i => !val.includes(i));
      this.colData.filter(i => {
        if (arr.indexOf(i.title) != -1) {
          i.isTrue = false;
        } else {
          i.isTrue = true;
        }
      });
      this.reload = Math.random()
    }
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      listSupplier(this.addDateRange(this.queryParams)).then(res => {
          this.supplierList = res.rows
          this.total = res.total
          this.loading = false
          // this.getSupplierlist()
      })
    },
    // 筛选列表
    getSearchList(){
      this.loading = true
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        status: 1, 
        approvalStatus: 1,
        searchValue:undefined
      },
      listSupplier(this.addDateRange(this.queryParams)).then(res => {
          this.supplierList = res.rows
          this.total = res.total
          this.loading = false
          // this.getSupplierlist()
      })
    },
    // getSupplierlist(){
    //   listSupplierlist().then(res => {
    //     this.supplierList.forEach(item => {
    //       let arr = res.rows.find(e =>{
    //         return e.id == item.sortId
    //       })
    //       // console.log(arr)
    //       if(arr != undefined){
    //         item.sortId = arr.sortName
    //       }
    //     })
    //   })
    // },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 新建
    supplierAdd(){
      this.$router.push({path:"/basedata/supplier/add"})
    },
    // 删除
    handleDelete(row){
      this.$modal.confirm('是否确认删除？').then(() => {
        deleteSupplier(row.id).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
      }).catch(() => {})
    },
    // 查看详情
    handleView(row) {
      if(this.type == "1"){
        if(row.status == '1' && row.approveStatus == "1"){
          this.$tab.closeOpenPage({ path: "/purchaseOrder/add", query:{ supplier: row } })
        }else{
          this.$modal.msgError('请选择已审核的供应商！')
        }
      }else if(this.type == 'supplier'){
        if(row.status == '1' && row.approveStatus == "1"){
          this.$tab.closeOpenPage({ path: "/purchaseOrder/edit", query:{ id: this.id, type: this.type, supplier: row, list: this.list } })
        }else{
          this.$modal.msgError('请选择已审核的供应商！')
        }
      }else{
        this.$router.push({path:"/basedata/supplier/detail",query:{id: row.id}})
      }
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.map(item => item.id)
    },
    // 导出
    btnExport(){
      let a = this.colData.filter(i => {
        return i.isTrue == true
      })
      let b = []
      a.forEach((item) => {
        this.tableHeaderData.forEach((data) => {
          if(item.title == data.label){
            b.push({name: data.prop, value: item.title})
          }
        })
      })
      const obj = b.reduce((result, item) => {
        result[item.name] = item.value
        return result
      }, {})
      if(this.ids.length > 0){
        let data = {
          ids: this.ids,
          headMap: obj,
          searchValue: this.queryParams.searchValue,
        }
        this.getExport(data)
      }else{
        let data = {
          headMap: obj,
          searchValue: this.queryParams.searchValue
        }
        this.getExport(data)
      }
    },
    getExport(data){
      this.$modal.loading("正在导出")
      exportSupplier(data).then(res => {
        this.$modal.closeLoading()
        let blob = new Blob([res], {
          type: res.type,
        })
        let downloadElement = document.createElement("a")
        let href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = "供应商列表.xls";
        document.body.appendChild(downloadElement)
        downloadElement.click()
        window.URL.revokeObjectURL(href)
      })
    }
  }
}
</script>